package com.xnyzc.lhy.resource.controller.coupon;


import com.xnyzc.lhy.common.component.heli.HeLiUrlContant;
import com.xnyzc.lhy.common.entity.CouponRv;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.entity.WorksheetRv;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import com.xnyzc.lhy.common.util.StringUtil;
import com.xnyzc.lhy.common.util.sign.AttestationTool;
import com.xnyzc.lhy.mis.entity.coupon.CouponBatchPublishParam;
import com.xnyzc.lhy.mis.entity.coupon.CouponEffectiveSearchParam;
import com.xnyzc.lhy.mis.entity.coupon.CouponPublishParam;
import com.xnyzc.lhy.mis.entity.coupon.CouponEffectiveResult;
import com.xnyzc.lhy.resource.entity.worksheet.param.DetectionToken;
import com.xnyzc.lhy.resource.feign.coupon.ICouponService;
import com.xnyzc.lhy.resource.feign.worksheet.IOaAuthService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券处理 前端控制器
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponController {
    @Autowired
    ICouponService iCouponService;

    @Autowired
    private IOaAuthService iOaAuthService;

    /**
     * 有效优惠券查询
     *
     * @return
     */
    @ApiOperation(value = "有效优惠券查询", notes = "有效优惠券查询")
    @GetMapping(value = "/queryEffectiveCouponList")
    public CouponRv queryEffectiveCouponList(@RequestBody CouponEffectiveSearchParam couponEffectiveSearchParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(couponEffectiveSearchParam, couponEffectiveSearchParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return CouponRv.wrapErrorCode(CouponErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(couponEffectiveSearchParam.getUserToken())
                || !this.getDetectionToken(couponEffectiveSearchParam.getUserToken())) {
            return CouponRv.wrapErrorCode(CouponErrorCode.userTookenVerification);
        }

        return iCouponService.queryEffectiveCouponList();
    }

    /**
     * 发放优惠券
     *
     * @return
     */
    @ApiOperation(value = "发放优惠券", notes = "发放优惠券")
    @PostMapping(value = "/publishCoupon")
    public CouponRv publishCoupon(@RequestBody CouponPublishParam couponPublishParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(couponPublishParam, couponPublishParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return CouponRv.wrapErrorCode(CouponErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(couponPublishParam.getUserToken())
                || !this.getDetectionToken(couponPublishParam.getUserToken())) {
            return CouponRv.wrapErrorCode(CouponErrorCode.userTookenVerification);
        }

        return iCouponService.publishCoupon(couponPublishParam);
    }

    /**
     * 批量发放优惠券
     *
     * @return
     */
    @ApiOperation(value = "批量发放优惠券", notes = "批量发放优惠券")
    @PostMapping(value = "/batchPublishCoupon")
    public CouponRv batchPublishCoupon(@RequestBody CouponBatchPublishParam couponBatchPublishParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(couponBatchPublishParam, couponBatchPublishParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return CouponRv.wrapErrorCode(CouponErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(couponBatchPublishParam.getUserToken())
                || !this.getDetectionToken(couponBatchPublishParam.getUserToken())) {
            return CouponRv.wrapErrorCode(CouponErrorCode.userTookenVerification);
        }

        return iCouponService.batchPublishCoupon(couponBatchPublishParam);
    }

    /**
     * 判断访问权限验证
     */
    private Boolean getDetectionToken(String userToken) {
        DetectionToken detectionToken = new DetectionToken();
        detectionToken.setToken(userToken);

        Rv result = iOaAuthService.getDetectionToken(detectionToken);

        if (result.getData() != null
                && !"".equals(StringUtil.isNullReturnEmpty(result.getData().toString()))) {
            return true;
        } else {
            return false;
        }
    }
}
